package com.five.util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class StringUtil {
    public static String getUploadDateDir() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        String dirPath =  year + "/" + month + "/" + day + "/";
        return dirPath;
        }

    public static String getTimeStampString(){
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        String dateName = df.format(calendar.getTime());
        return dateName;
    }


    /**
     * 获取制定天数前的日期
     * @param date
     * @param beforeDays
     * @return
     */
    public static Date getBeforeDaysDate(Date date, int beforeDays){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -beforeDays);
        return calendar.getTime();
    }


    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> getBeforeDaysList(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList();
//        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = intervals-1; i >= 0; i--) {
            pastDaysList.add(getPastDate(i));
//            fetureDaysList.add(getFetureDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static void main(String[] args) {
//        Date beforeDaysDate = getBeforeDaysDate(new Date(), 7);
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        String result = format.format(beforeDaysDate);
//        System.out.println(result);

        ArrayList<String> test = getBeforeDaysList(7);
        for(String str:test){
            System.out.println(str);
        }
    }

    }
